﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla.Data;
using Csla;

namespace Kiosk.Library
{
    public partial class PresentationReadOnlyList
    {

        /// <summary>
        /// Returns all presentations that are enabled, enabled presentations can be displayed
        /// </summary>
        static public PresentationReadOnlyList GetAllEnabledPresentations()
        {
            return DataPortal.Fetch<PresentationReadOnlyList>(new SingleCriteria<PresentationReadOnlyList, string>("Enabled"));
        }

        /// <summary>
        /// Returns all presentations that are disabled
        /// </summary>
        static public PresentationReadOnlyList GetAllDisabledPresentations()
        {
            return DataPortal.Fetch<PresentationReadOnlyList>(new SingleCriteria<PresentationReadOnlyList, string>("Disabled"));
        }


        int _currentIndex = -1;
        /// <summary>
        /// Cycles through all the presentations one by one in a loop
        /// </summary>
        public PresentationInfo GetNextPresentation()
        {
            _currentIndex++;
            if (_currentIndex >= this.Count)
            {
                return null;
            }
            else
            {
                return this[_currentIndex];
            }
                    
        }

        private void DataPortal_Fetch(SingleCriteria<PresentationReadOnlyList, string> criteria)
        {
            FetchAllByStatus(criteria.Value);
        }

        private void FetchAllByStatus(string statusName)
        {
            RaiseListChangedEvents = false;
            using (var ctx = ContextManager<Kiosk.Data.KioskDataContext>.GetManager(Database.ConnectionString))
            {
                var data = from o in ctx.DataContext.Presentation
                           where o.PresentationStatus.Name == statusName
                           select o;
                IsReadOnly = false;
                foreach (var d in data)
                    this.Add(new PresentationInfo(d.PresentationID, d.PresentationTypeID, d.PresentationStatusID, d.Name));
                IsReadOnly = true;
            }
            RaiseListChangedEvents = true;
        }
    }
}
